package com.search.analysis.impl;

import com.search.FxApplication;
import com.search.analysis.Analysis;
import com.search.entity.SearchContentEntity;
import com.search.utils.ChromeDriverUtil;
import com.search.utils.MapAndBean;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;


@Component
public class Wu1ctoAnalysis implements Analysis {

    @Override
    public String getData(String keyWord) {
        String url = "http://so.51cto.com/?sever_type=2&keywords="+keyWord;
        ChromeDriverUtil build = ChromeDriverUtil.buildHide(FxApplication.chromedriver);
        ChromeDriver driver = build.getDriver();
        driver.navigate().to(url);
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
        Boolean until = wait.until(webDriver -> {
            WebElement element = webDriver.findElement(By.cssSelector(".result-list .post-item"));
            return element.isDisplayed();
        });
        if (!until) {
            System.out.println("没有找到元素");
            build.stop();
            return null;
        }
        WebElement element4 = driver.findElement(By.xpath("//*"));
        String source_code = element4.getAttribute("outerHTML");
        build.stop();
        return source_code;
    }

    @Override
    public List<Map<String, Object>> extract(String data) {
        List<Map<String, Object>> list = new CopyOnWriteArrayList<>();
        Document parse = Jsoup.parse(data);
        Elements select = parse.select(".result-list .post-item");
        select.stream().parallel().forEach(element -> {
            try {
                Map<String, Object> map=new HashMap<>();
                Element title = element.selectFirst("a");
                map.put("title",title.text());
                map.put("url",title.attr("href"));
                Element content = element.selectFirst(".post-info a");
                if (content!=null){
                    map.put("content",content.text());
                    map.put("author","51CTO");
                    map.put("source","51CTO");
                    Element time = element.selectFirst(".post-info .post-time");
                    if (time!=null){
                        map.put("time",time.text());
                    }
                    list.add(map);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        System.out.println("51CTO查询到的数量:"+list.size());
        return list;
    }

    @Override
    public List<SearchContentEntity> rinse(List<Map<String, Object>> list) {
        List<SearchContentEntity> collect = list.stream().parallel().map(map -> {
            if (map.get("url") == null||map.get("title") == null) {
               return null;
            }
            SearchContentEntity searchContentEntity = MapAndBean.mapToDomain(map, SearchContentEntity.class);
        return searchContentEntity;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SearchContentEntity> process(List<SearchContentEntity> list) {
        return list;
    }


}
